package com.hl.controller;

import com.hl.domain.Heartknowledge;
import com.hl.service.HeartknowledgeService;
import com.hl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: WenYong
 * @Date: 2024/9/4 14:20
 * @Version: 1.0
 */
@RestController
@RequestMapping("/Heartknowledge")
public class HeartknowledgeController {
    @Autowired
    private HeartknowledgeService heartknowledgeService;

    // 查询所有心理健康的文章
    @GetMapping("/getAllArticles")
    public List<Heartknowledge> getAllArticles(){
        return heartknowledgeService.getAllList();
    }

 @GetMapping("down")
    public void down(String filename, HttpServletResponse response) throws IOException {
        FileInputStream in = new FileInputStream("d:\\picture\\" + filename);
        ServletOutputStream out = response.getOutputStream();

        byte[] b = new byte[1024];
        int i = in.read(b);
        while(i!= -1){
            out.write(b,0,i);
            i = in.read(b);
        }
        out.close();
        in.close();
    }

    @PostMapping("upload")
    public R upload(MultipartFile file) throws IOException {
        String path = "D:\\picture\\";

        String originalFilname = file.getOriginalFilename();
        System.out.println(originalFilname);
        String ext = originalFilname.substring(originalFilname.lastIndexOf("."));
        String filename = UUID.randomUUID().toString()+ext;
        path += filename;
        file.transferTo(new File(path));

        return R.success(filename);
    }


    //增加
    @PostMapping("/insert")
    public R insert(Heartknowledge heartknowledge){
        heartknowledge.setCreatetime(new Date());
        int  i=  heartknowledgeService.insertOne(heartknowledge);
        if(i==1){
            return  R.success("1");
        }else{
            return  R.fail(i);
        }
    }

    @GetMapping("selectAll")
    public List<Heartknowledge> selectAll(int pageNum,int pageSize){

        List<Heartknowledge> heartknowledgeList = heartknowledgeService.heartknowledgeList(pageNum,pageSize);
        return heartknowledgeList;
    }

    //根据标题名字查找
    @GetMapping("selectByName")
    public List<Heartknowledge> selectByName(String name, int pageNum, int pageSize, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,@DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime){
        return heartknowledgeService.selectByName(name,pageNum, pageSize,startTime,endTime);
    }

    //根据id删除
    @DeleteMapping("deleteById")
    public R deleteById(Long id){
        int  i=  heartknowledgeService.deleteById(id);
        return i==0 ? R.fail("删除失败"):R.success("删除成功");
    }

    //根据id更新
    @PutMapping("updateById")
    public R updateById(@RequestBody Heartknowledge heartknowledge){
        System.out.println(heartknowledge);
        int i = heartknowledgeService.updateById(heartknowledge);
        return i==0 ? R.fail("更新失败"):R.success("更新成功");
    }

    @GetMapping("selectOne")
    public Heartknowledge selectOne(Long id){
        Heartknowledge heartknowledge = heartknowledgeService.selectOne(id);
        return heartknowledge;
    }

    //根据id查找text
    @GetMapping("selectText")
    public String selectText(Long id){
        return heartknowledgeService.selectText(id);
    }
}
